package rsj.service.export.org.produce;

import com.rlyy.aibase.utils.base.Node;
import com.rlyy.aibase.utils.base.TableNode;
import common.util.DateTimeFormatterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import rsj.api.org.entity.UsageReportParam;
import rsj.api.org.importdata.ImportEasyExcelInvoke;
import rsj.service.export.org.IOrgDownloadDetail;
import rsj.service.usage.IUsageTemplateMethod;
import rsj.sql.org.OrgProduceDetailDao;

import java.time.LocalDate;
import java.util.Objects;
import java.util.Optional;

/**
 * 机构管理-生产环境导出全部详情
 *
 * @author yuanqinglong
 * @since 2021/3/5 14:00
 */
@Component
public class OrgProduceExportAllDetail implements IOrgDownloadDetail {


    @Autowired
    private OrgProduceDetailDao orgProduceDetailDao;



    @Override
    public TableNode queryTableByPage(UsageReportParam usageReportParam) {
        return orgProduceDetailDao.produceExportAllDetail(usageReportParam);
    }



    @Override
    public Node totalStatistics(UsageReportParam usageReportParam) {
        return orgProduceDetailDao.produceExportAllCount(usageReportParam);
    }



    /**
     * 生产环境导出全部详情
     *
     * @param usageReportParam 参数
     * @return sql
     */
    @Override
    public String generateQueryStatement(UsageReportParam usageReportParam) {
        String sql = " SELECT log_ip,log_reqtime, log_reqsernumber,stat_env,CASE log_iscost WHEN '1' THEN '是' ELSE '否' END log_iscost, " +
                "        stat_org_sname, log_orgp_id,log_prod_uid, stat_prod_name FROM sys.oper_log test  LEFT JOIN ( " +
                "           SELECT stat_date,stat_orgp_id,stat_org_sname,regexp_split_to_table(stat_prod_name,',') AS stat_prod_name,stat_env,regexp_split_to_table(stat_prod_uid,',') AS split_prod_uid FROM sys.sys_stat " +
                "     ) split ON split_prod_uid = test.log_prod_uid AND test.log_orgp_id = split.stat_orgp_id " +
                "            LEFT JOIN sys.sys_authon AS au ON au.aut_keyid = log_user_id LEFT JOIN sys.sys_org ON org_id = aut_org_id" +
                " WHERE aut_org_id = #{statOrgId} " +
                " AND stat_env = '" + IUsageTemplateMethod.PRODUCE_ENVIRONMENT + "' AND log_iscost = '" + ImportEasyExcelInvoke.IS_COST + "'";
        // 没选择统计日期 统计今天之前的数据
        LocalDate statDate = Objects.nonNull(usageReportParam.getStatDate()) ? usageReportParam.getStatDate() : LocalDate.now().minusDays(1);
        usageReportParam.setStatDate(statDate);
        sql += " AND stat_date <= #{statDate}";
        return sql;
    }


    /**
     * sql统计
     *
     * @param usageReportParam 使用情况报告参数
     * @return {@link String}
     */
    public String sqlStatistics(UsageReportParam usageReportParam) {
        String sql = " SELECT COUNT(*) FROM sys.oper_log test  LEFT JOIN ( " +
                "           SELECT stat_date,stat_orgp_id,stat_org_sname,regexp_split_to_table(stat_prod_name,',') AS stat_prod_name,stat_env,regexp_split_to_table(stat_prod_uid,',') AS split_prod_uid FROM sys.sys_stat " +
                "     ) split ON split_prod_uid = test.log_prod_uid AND test.log_orgp_id = split.stat_orgp_id " +
                "            LEFT JOIN sys.sys_authon AS au ON au.aut_keyid = log_user_id LEFT JOIN sys.sys_org ON org_id = aut_org_id" +
                " WHERE aut_org_id = #{statOrgId} " +
                " AND stat_env = '" + IUsageTemplateMethod.PRODUCE_ENVIRONMENT + "' AND log_iscost = '" + ImportEasyExcelInvoke.IS_COST + "'";
        // 没选择统计日期 统计今天之前的数据
        LocalDate statDate = Objects.nonNull(usageReportParam.getStatDate()) ? usageReportParam.getStatDate() : LocalDate.now().minusDays(1);
        usageReportParam.setStatDate(statDate);
        sql += " AND stat_date <= #{statDate}";
        return sql;
    }


    /**
     * 生成导出的文件名称
     *
     * @param usageReportParam usageReportParam参数
     * @return 文件名
     */
    @Override
    public String generateExportFileName(UsageReportParam usageReportParam) {
        String deadline = Optional.ofNullable(usageReportParam.getStatDate()).map(DateTimeFormatterUtil::localDateFormatterToStringDate).orElseGet(() -> DateTimeFormatterUtil.localDateFormatterToStringDate(LocalDate.now()));
        return this.queryOrgNameById(usageReportParam.getStatOrgId()) + "-生产环境-截至" + deadline + "-全部详情.xlsx";
    }

}
